﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatterns.TemplateMethodPatterns
{
    class TemplateMethodClient
    {
        static void Main(string[] args)
        {
            //实例化模板方法1
            TemplateMethodBase templatemethod1 = new TemplateMethod1();
            templatemethod1.Run();

            //实例化模板方法2
            TemplateMethodBase templatemethod2 = new TemplateMethod2();
            templatemethod2.Run();

            Console.Read();
        }
    }

    /// <summary>
    /// 公共方法被封装到基类中
    /// </summary>
    public abstract class TemplateMethodBase {
        private bool flag = false;
        protected abstract void action1();
        protected abstract void action2();
        protected abstract void action3();
        public void Run() {
            action1();
            action2();
            if (flag)
                action1();
            action3();
        }
        public TemplateMethodBase(bool _flag = false)
        {
            flag = _flag;
        }
    }

    /// <summary>
    /// 非公共（抽象）方法需要被子类重写
    /// </summary>
    public class TemplateMethod1 : TemplateMethodBase {
        public TemplateMethod1(bool _flag = true) : base(_flag)
        {
        }

        protected override void action1()
        {
            Console.WriteLine("TemplateMethod1 action1");
        }

        protected override void action2()
        {
            Console.WriteLine("TemplateMethod1 action2");
        }

        protected override void action3()
        {
            Console.WriteLine("TemplateMethod1 action3");
        }
    }

    /// <summary>
    /// 非公共（抽象）方法需要被子类重写
    /// </summary>
    public class TemplateMethod2 : TemplateMethodBase
    {
        public TemplateMethod2(bool _flag = false) : base(_flag)
        {
        }

        protected override void action1()
        {
            Console.WriteLine("TemplateMethod2 action1");
        }

        protected override void action2()
        {
            Console.WriteLine("TemplateMethod2 action2");
        }

        protected override void action3()
        {
            Console.WriteLine("TemplateMethod2 action3");
        }
    }
}
